package com.wlr.study.huihui;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Stack;

/**
 * @author lerong.wang
 * @version 1.0.0
 * @description
 * @date 2025/4/23 16:12
 */
public class Huihui1034 {

    public List<String> outputSimpleResult(String input) {
        Stack<Character> stack = new Stack<>();
        String[] s = input.split(" ");
        String r1 = s[0];
        String r2 = s[1];
        String r1Simple = simpleResult(r1);
        String r2Simple = simpleResult(r2);
        stack.push('/');
        stack.push('*');
        stack.push('-');
        stack.push('+');
        while (!stack.isEmpty()) {
            System.out.println(calculate(r1Simple, r2Simple, stack.pop()));
        }
        return null;
    }

    private String calculate(String r1Simple, String r2Simple, Character operator) {
        int r1Son = getNumber(r1Simple, 0);
        int r1Mot = getNumber(r1Simple, 1);
        int r2Son = getNumber(r2Simple, 0);
        int r2Mot = getNumber(r2Simple, 1);
        return switch (operator) {
            case '+', '-' -> getResultByOperate(operator, r1Son, r1Mot, r2Son, r2Mot);
            case '*', '/' -> getResultByMulOrDiv(r1Son, r2Son, r1Mot, r2Mot, operator);
            default -> throw new IllegalStateException("Unexpected value: " + operator);
        };

    }

    private String getResultByOperate(Character operator, int r1Son, int r1Mot, int r2Son, int r2Mot) {
        if (r1Mot != r2Mot) {
            int tempMot = r1Mot * r2Mot;
            int tempR1Son = r1Son * r2Mot;
            int tempR2Son = r2Son * r1Mot;
            return getResultByAddOrSub(tempR1Son, tempR2Son, tempMot, operator);
        } else {
            return getResultByAddOrSub(r1Son, r2Son, r1Mot, operator);
        }
    }

    public String getResultByMulOrDiv(Integer son1, Integer son2, Integer mot1, Integer mot2, Character operator) {
        if (operator == '*') {
            return getString(son1, mot2, mot1, son2);
        } else {
            if (son2 == 0) {
                return "Inf";
            }
            return getString(son1, son2, mot1, mot2);
        }
    }

    private String getString(Integer son1, Integer son2, Integer mot1, Integer mot2) {
        int tempResultSon = son1 * mot2;
        int tempResultMot = mot1 * son2;
        int tempResultMol = Math.abs(tempResultSon % tempResultMot);
        int tempResultDiv = tempResultSon / tempResultMot;
        if (Math.abs(tempResultDiv) < 1) {
            if (tempResultMot<0){
                return simpleResult("-"+tempResultMol + "/" + Math.abs(tempResultMot));
            }
            return simpleResult(tempResultMol + "/" + tempResultMot);
        } else {
            return tempResultDiv + " " + simpleResult(tempResultMol + "/" + tempResultMot);
        }
    }

    public String getResultByAddOrSub(Integer son1, Integer son2, Integer mother, Character operator) {
        if (operator == '+') {
            int tempResultMol = Math.abs((son1 + son2) % mother);
            int tempResultDiv = (son1 + son2) / mother;
            return tempResultDiv + " " + tempResultMol + "/" + mother;
        } else {
            int tempResultMol = Math.abs((son1 - son2) % mother);
            int tempResultDiv = (son1 - son2) / mother;
            return tempResultDiv + " " + tempResultMol + "/" + mother;
        }

    }

    public Integer getNumber(String needToConvert, int index) {
        return Integer.parseInt(needToConvert.split("/")[index]);
    }

    public String simpleResult(String r) {
        int son = getNumber(r, 0);
        int mother = getNumber(r, 1);
        int max = Math.max(son, mother);
        List<Integer> maxList = new ArrayList<>();
        for (int i = 2; i <= max; i++) {
            if (son % i == 0 && mother % i == 0) {
                maxList.add(i);
            }
        }
        if (maxList.isEmpty()) {
            return son + "/" + mother;
        }

        int i = maxList.stream().max(Comparator.comparingInt(i2 -> i2)).get();
        return (son / i) + "/" + (mother / i);
    }

    public String isEliminated(String input) {
        int son = getNumber(input, 0);
        int mother = getNumber(input, 1);
        if (son % mother == 0) {
            return String.valueOf(son / mother);
        } else {
            return input;
        }
    }

    public static void main(String[] args) {
        Huihui1034 huihui1034 = new Huihui1034();
//        System.out.println(huihui1034.simpleResult("-8/4"));
        System.out.println(huihui1034.outputSimpleResult("5/3 0/6"));
    }

}